﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Admin.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.ComMethod;
using System.Globalization;

namespace FortuneHeaven.Admin.Pages.GameManager
{
    public partial class UserInforDetail : FortuneHeaven.Admin.Common.PageBase
    {
        public override string PageID
        {
            get { return "F56"; }
        }
        private string pre = "";
        private int id = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ID >= 0)
                {
                    AccountsInfoBO bo = new AccountsInfoBO();
                    AccountsInfo g = bo.GetAcount(ID);
                    UserInforBO userbo = new UserInforBO();
                    UserInfor user = userbo.GetUserInfor(ID);
                    this.txtArea.Text = g.Add_Area;
                    this.txtBankName.Text = g.Add_BankName;
                    this.txtBankNo.Text = g.Add_BankID;
                    this.txtLastLogin.Text = g.LastLogonDate.ToString("yyyy/MM/dd");
                    this.txtLastLoginIP.Text = g.LastLogonIP;
                    this.txtMachine.Text = g.MachineSerial;
                    this.txtMobieNo.Text = g.Add_MobilePhone;
                    this.txtOverDue.Text = g.MemberOverDate.ToString("yyyy/MM/dd");
                    this.txtRegisterDate.Text = g.RegisterDate.ToString("yyyy/MM/dd");
                    this.txtRegisterIP.Text = g.RegisterIP;
                    this.txtUserName.Text = g.Accounts;

                    this.rbFixedMachine.SelectedValue = g.MoorMachine.ToString();
                    this.rbGender.SelectedValue = g.Gender.ToString();
                    this.rbManage.SelectedValue = g.MasterOrder.ToString();
                    this.rbShut.SelectedValue = (g.StunDown ? 1 : 0).ToString();
                    this.rbMember.SelectedValue = g.MemberOrder.ToString();
                    this.rbStopServer.SelectedValue = (g.Nullity ? 1 : 0).ToString();

                    this.lbRegAcount.Text = g.RegAccounts;
                    this.lbGameID.Text = g.GameID.ToString();
                    if (user != null)
                    {
                        this.txtPwdQuestion.Text = user.RegisterQuestion;
                        this.txtPwdAnswer.Text = user.RegisterAnswer;
                        this.txtMail.Text = user.RegisterEmail;
                    }
                    foreach(ListItem item in this.ckManageAuth.Items)
                    {
                        if( (int.Parse(item.Value) & g.MasterRight) != 0)
                        {
                            item.Selected =true;
                        }
                    }

                    foreach (ListItem item in this.ckUserAuth.Items)
                    {
                        if ((int.Parse(item.Value) & g.UserRight) != 0)
                        {
                            item.Selected = true;
                        }
                    }
                }
            }
        }

        public new int ID
        {
            get
            {
                if (id<0)
                {
                    id= string.IsNullOrEmpty(Request["ID"]) ? -1 : int.Parse(Request["ID"]);
                }
                if (id < 0)
                {
                    string account = Request["Accounts"];
                    if (!string.IsNullOrEmpty(account))
                    {
                        AccountsInfoBO bo = new AccountsInfoBO();
                         AccountsInfo infor  =bo.GetLightAcountInfor(account);
                         if (infor != null)
                         {
                             id = infor.UserID;
                         }
                    }
                }
                return id;
            }
        }

        public string Pre
        {
            get
            {
                if (!string.IsNullOrEmpty(pre))
                    return pre;
                else
                    pre = Server.UrlDecode(Request["pre"]);
                return pre;
            }
        }


        public void Save(object sender, EventArgs arg)
        {
            if (!this.IsValid)
            {
                return;
            }

            AccountsInfoBO bo = new AccountsInfoBO();
            AccountsInfo g;
            if (ID >= 0)
            {
                g = bo.GetAcount(ID);

            }
            else
            {
                g = new AccountsInfo();
            }
            g.Add_Area = this.txtArea.Text;
            g.Add_BankName = this.txtBankName.Text;
            g.Add_BankID = txtBankNo.Text;
            g.LastLogonDate = DateTime.Parse(this.txtLastLogin.Text); 
            g.LastLogonIP = txtLastLoginIP.Text;
            g.MachineSerial = txtMachine.Text;
            g.Add_MobilePhone = txtMobieNo.Text;
            g.MemberOverDate =  DateTime.Parse(this.txtOverDue.Text);

            g.RegisterDate = DateTime.Parse(this.txtRegisterDate.Text);
            g.RegisterIP = txtRegisterIP.Text;
            g.Accounts = txtUserName.Text;

            g.MoorMachine = SafePare(rbFixedMachine.SelectedValue);
            g.Gender = SafePare(rbGender.SelectedValue);
            g.MasterOrder = SafePare(rbManage.SelectedValue);
            g.StunDown = SafePare(rbShut.SelectedValue) == 1 ? true : false;
            g.MemberOrder = SafePare(rbMember.SelectedValue);
            g.Nullity = SafePare(rbStopServer.SelectedValue) == 1 ? true : false;


            if (!string.IsNullOrEmpty(txtLoginPassword.Text))
            {
                //需修改
                g.LogonPass = CommonEncrypt.ClientEncrypt(txtLoginPassword.Text);
            }

            int manageCount = 0;
            int userCount = 0;
            foreach (ListItem item in this.ckManageAuth.Items)
            {
                if (item.Selected)
                {
                    manageCount = manageCount | int.Parse(item.Value);
                }
            }

            foreach (ListItem item in this.ckUserAuth.Items)
            {
                if (item.Selected)
                {
                    userCount = userCount | int.Parse(item.Value);
                }
            }
            g.MasterRight = manageCount;
            g.UserRight = userCount;
            if (ID >= 0)
            {
                bo.Save(g);
                Response.Write(string.Format("<Html> <script>alert('修改成功');document.location ='{0}'; </script> </HTML>", Pre));

            }
            else
            {
                bo.Add(g);
                Response.Write(string.Format("<Html> <script>alert('新增成功');document.location ='{0}'; </script> </HTML>", Pre));
                Response.Flush();
            }
        }

        private int SafePare(string value)
        {
            int k = 0;
            int.TryParse(value,out k);
            return k;
        }

        public void CancelSave(object sender, EventArgs arg)
        {
            Response.Redirect(Pre);
        }
    }
}